<?php
namespace app\models;
use ac\component\tool\Tool;
use app\models\Basic;

use Yii;
use yii\helpers\Html;

class OrderInfo extends Basic
{
    
    public static function tableName()
    {
        return '{{%pj8_order_info}}';
    }

    public function rules()
    {
        return [
            ['member_id', 'integer', 'message' => 'member_id必须为数字，或数字字符串', 
                         'min'=>1, 'tooSmall' => 'member_id最小存储1'],
            ['order_sn', 'default', 'value' => ''],
            ['order_sn', 'string', 'message' => 'order_sn必须是字符串', 
                         'max' => 64, 'tooLong' => 'order_sn最长64位'],
            ['order_status', 'integer', 'message' => 'order_status必须为数字，或数字字符串'],
            ['order_status', 'in', 'range' => array(1, 2, 3, 4, 5), 'message' => 'order_status值必须在1~5中选择'],
            ['shipping_status', 'integer', 'message' => 'shipping_status必须为数字，或数字字符串'],
            ['shipping_status', 'in', 'range' => array(1, 2, 3, 4, 5, 6), 'message' => 'shipping_status值必须在1~6中选择'],
            ['pay_mode', 'integer', 'message' => 'pay_mode必须为数字，或数字字符串'],
            ['pay_mode', 'in', 'range' => array(1, 2, 3), 'message' => 'pay_mode值必须在1~3中选择'],
            ['deliver_id', 'integer', 'message' => 'deliver_id必须为数字，或数字字符串', 
                            'min'=>0, 'tooSmall' => 'deliver_id最小存储0', 
                            'max'=>18446744073709551615, 'tooBig' => 'deliver_id最大存储18446744073709551615'],

            ['consignee', 'string', 'message' => 'consignee必须是字符串', 
                         'max' => 64, 'tooLong' => 'consignee最长64位'],
            ['sex', 'integer', 'message' => 'sex必须为数字，或数字字符串'],
            ['sex', 'in', 'range' => array(1, 2), 'message' => 'sex值必须在1~2中选择'],
            // ['phone', 'required', 'message' => 'phone不能为空', 'on' => 'add'],
            ['phone', 'string', 'message' => 'phone必须是字符串',
                                'min' => 1, 'tooShort' => 'phone最短1位',
                                'max' => 18, 'tooLong' => 'phone最长18位'],
            // ['province', 'default', 'value' => ''],
            ['province', 'string', 'message' => 'province必须是字符串', 
                         'max' => 16, 'tooLong' => 'province最长16位'],
            // ['city', 'default', 'value' => ''],
            ['city', 'string', 'message' => 'city必须是字符串', 
                     'max' => 16, 'tooLong' => 'city最长16位'],
            // ['district', 'default', 'value' => ''],
            ['district', 'string', 'message' => 'district必须是字符串', 
                         'max' => 16, 'tooLong' => 'district最长16位'],
            ['address', 'default', 'value' => ''],
            ['address', 'string', 'message' => 'address必须是字符串', 
                         'max' => 128, 'tooLong' => 'address最长128位'],

            ['source', 'integer', 'message' => 'source必须为数字，或数字字符串'],
            ['source', 'in', 'range' => array(1, 2), 'message' => 'source值必须在1~2中选择'],

            ['experts_id', 'integer', 'message' => 'experts_id必须为数字，或数字字符串', 
                         'min'=>0, 'tooSmall' => 'experts_id最小存储0'],
            ['auth_type', 'integer', 'message' => 'auth_type必须为数字，或数字字符串'],
            ['auth_type', 'in', 'range' => array(1, 2, 3), 'message' => 'auth_type值必须在1~3中选择'],
            ['order_amount', 'integer', 'message' => 'order_amount必须为数字，或数字字符串', 
                            'min'=>0, 'tooSmall' => 'order_amount最小存储0', 
                            'max'=>18446744073709551615, 'tooBig' => 'order_amount最大存储18446744073709551615'],

            ['confirm_time', 'default', 'value' => '1000-01-01 00:00:00', 'on' => 'add'],
            ['confirm_time', 'date', 'format'=>'yyyy-MM-dd HH:mm:ss', 'message' => 'confirm_time非yyyy-MM-dd HH:mm:ss格式'],
            ['add_time', 'default', 'value' => '1000-01-01 00:00:00', 'on' => 'add'],
            ['add_time', 'date', 'format'=>'yyyy-MM-dd HH:mm:ss', 'message' => 'add_time非yyyy-MM-dd HH:mm:ss格式'],
        ];
    }


    /*添加*/
    public function addOrder($data)
    {
        // P($data);changeCatidFormat
        $this->scenario = 'add';
        if(!isset($data['OrderInfo']['add_time']) or empty($data['OrderInfo']['add_time'])){
            $data['OrderInfo']['add_time'] = date('Y-m-d H:i:s', time());
        }
        $data['OrderInfo']['order_sn'] = $this->createOrderSn();
        // P($data);
        if($this->load($data) and $this->validate()){
            // P($this);
            if($this->save(false)){
                $id = $this->getPrimaryKey();
                return $id;
            }
        }
        return false;
    }

    /*
    修改
    $data       修改的数据
    */
    public function modifyOrder($data, $id)
    {
        // P($data);
        $order = self::find()->where('order_id = :id', [':id' => $id])->one();
        // P($order);
        // P($data);
        if(!is_null($order)){
            if($this->load($data) and $this->validate()){
                // P($data);
                if(isset($this->order_status)){
                    $order->order_status = $this->order_status;
                }
                if(isset($this->shipping_status)){
                    $order->shipping_status = $this->shipping_status;
                }
                if(isset($this->deliver_id)){
                    $order->deliver_id = $this->deliver_id;
                }
                if(isset($this->source)){
                    $order->source = $this->source;
                }
                if(isset($this->order_amount)){
                    $order->order_amount = $this->order_amount;
                }
                if(isset($this->order_status) and $order->order_status == 2){
                    $order->confirm_time = date('Y-m-d H:i:s', time());
                }
                // P($order);
                if($order->save(false)){
                    return true;
                }
            }
        };
        return false;
    }


    public function getMember()
    {
        $info = $this->hasOne(Member::className(), ['member_id' => 'member_id'])
                    ->select(['{{%pj8_member}}.member_id', 'nickname', 'headimgurl']);
        return $info;
    }


    /*
    产生唯一订单号
    $len        长度
    */
    private function createOrderSn($len = 8){
        $order_sn = str_pad(date('Ym', time()).mt_rand(1000, 9999).date('d', time()), $len, '0', STR_PAD_LEFT);//补零
        $orderInfo = self::find()->where('order_sn = :sn', [':sn' => $order_sn])->one();
        return $orderInfo?$this->createOrderSn():$order_sn;  //如果商品货号重复则重新生成  
    }

    
}